import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/overview',
    children: [{
      path: 'overview',
      name: '总览',
      component: () => import('@/views/overview/index'),
      meta: { title: '总览', icon: 'dashboard' }
    }]
  },

  {
    path: '/acquisitionManagement',
    component: Layout,
    redirect: '/acquisition',
    name: 'acquisition',
    meta: { title: '申领管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'acquisition',
        name: 'acquisition',
        component: () => import('@/views/acquisitionManagement/acquisition/index'),
        meta: { title: '申领物资申请', icon: 'table' }
      },
      {
        path: 'giveback',
        name: 'giveback',
        component: () => import('@/views/acquisitionManagement/giveback/index'),
        meta: { title: '归还物资', icon: 'table' }
      },
      {
        path: 'audit',
        name: 'audit',
        component: () => import('@/views/acquisitionManagement/audit/index'),
        meta: { title: '审批申领单', icon: 'table' }
      }
    ]
  },

  {
    path: '/purchase',
    component: Layout,
    redirect: '/purchase',
    name: 'purchase',
    meta: { title: '采购管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'provider',
        name: 'provider',
        component: () => import('@/views/purchase/provider/index'),
        meta: { title: '供应商管理', icon: 'table' }
      },
      {
        path: 'purchaseorder',
        name: 'purchaseorder',
        component: () => import('@/views/purchase/purchaseorder/index'),
        meta: { title: '采购订单管理', icon: 'table' },
        children: [
          {
            path: 'purchaseorder',
            name: 'purchaseorder',
            component: () => import('@/views/purchase/purchaseorder/purchaseorder/index'),
            meta: { title: '采购订单管理', icon: 'table' }
          },
          {
            path: 'audit',
            name: 'audit',
            component: () => import('@/views/purchase/purchaseorder/audit/index'),
            meta: { title: '审批采购订单', icon: 'table' }
          }
        ]
      },
      {
        path: 'arrival',
        name: 'arrival',
        component: () => import('@/views/purchase/arrival/index'),
        meta: { title: '采购到货管理', icon: 'table' }
      }
    ]
  },

  {
    path: '/warehouse',
    component: Layout,
    redirect: '/warehouse/information',
    name: 'warehouse',
    meta: { title: '库存管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'information',
        name: 'information',
        component: () => import('@/views/warehouse/information/index'),
        meta: { title: '仓库信息管理', icon: 'table' },
        redirect: '/warehouse/information/warehouse',
        children: [
          {
            path: 'warehouse',
            name: 'warehouse',
            component: () => import('@/views/warehouse/information/warehouse/index'),
            meta: { title: '仓库信息管理', icon: 'table' }
          },
          {
            path: 'inventory',
            name: 'inventory',
            component: () => import('@/views/warehouse/information/inventory/index'),
            meta: { title: '库存管理', icon: 'table' }
          }
        ]
      },
      {
        path: 'material',
        name: 'material',
        component: () => import('@/views/warehouse/material/index'),
        meta: { title: '物资信息管理', icon: 'table' }
      },
      {
        path: 'inspection',
        name: 'inspection',
        component: () => import('@/views/warehouse/inspection/index'),
        meta: { title: '物资质检管理', icon: 'table' }
      },
      {
        path: 'inwarehouse',
        name: 'inwarehouse',
        component: () => import('@/views/warehouse/inwarehouse/index'),
        meta: { title: '入库单管理', icon: 'table' }
      },
      {
        path: 'outwarehouse',
        name: 'outwarehouse',
        component: () => import('@/views/warehouse/outwarehouse/index'),
        meta: { title: '出库单管理', icon: 'table' }
      }
    ]
  },
  {
    path: '/analysis',
    component: Layout,
    redirect: '/analysis',
    name: 'analysis',
    meta: { title: '统计分析', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'inoutwarehouse',
        name: 'inoutwarehouse',
        component: () => import('@/views/analysis/inoutwarehouse/index'),
        meta: { title: '出入库统计报表', icon: 'table' }
      },
      {
        path: 'acquisition',
        name: 'acquisition',
        component: () => import('@/views/analysis/acquisition/index'),
        meta: { title: '申领统计报表', icon: 'table' }
      },
      {
        path: 'giveback',
        name: 'giveback',
        component: () => import('@/views/analysis/giveback/index'),
        meta: { title: '归还统计报表', icon: 'table' }
      },
      {
        path: 'purchase',
        name: 'purchase',
        component: () => import('@/views/analysis/purchase/index'),
        meta: { title: '采购统计报表', icon: 'table' }
      },
      {
        path: 'warehouse',
        name: 'warehouse',
        component: () => import('@/views/analysis/warehouse/index'),
        meta: { title: '库存统计报表', icon: 'table' }
      },
      {
        path: 'unqualified',
        name: 'unqualified',
        component: () => import('@/views/analysis/unqualified/index'),
        meta: { title: '不合格到货统计报表', icon: 'table' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system',
    name: 'system',
    meta: { title: '系统管理', icon: 'el-icon-s-help', roles: ['admin', 'edit'] },
    children: [
      {
        path: 'emp',
        name: 'emp',
        component: () => import('@/views/system/emp/index'),
        meta: { title: '员工信息管理', icon: 'table', roles: ['admin', 'edit'] }
      },
      {
        path: 'dept',
        name: 'dept',
        component: () => import('@/views/system/dept/index'),
        meta: { title: '部门信息管理', icon: 'table', roles: ['admin', 'edit'] }
      }
    ]
  },
  // {
  //   path: '/icon',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/icons/index'),
  //       name: 'Icons',
  //       meta: { title: 'Icons', icon: 'icon', noCache: true }
  //     }
  //   ]
  // },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
